package edu.unf.cen6940.dao;

import edu.unf.cen6940.domain.Category;
import edu.unf.cen6940.domain.Faculty;
import edu.unf.cen6940.domain.Facultyresearch;
import edu.unf.cen6940.domain.Generatedproduct;
import edu.unf.cen6940.domain.Researchkeyword;
import edu.unf.cen6940.domain.Thesis;
import edu.unf.cen6940.domain.Thesiscommittee;
import edu.unf.cen6940.domain.Thesistool;

import java.sql.Date;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashSet;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Set;

import javax.persistence.EntityManager;
import javax.persistence.NoResultException;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
import javax.persistence.TypedQuery;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Join;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;

import org.skyway.spring.util.dao.AbstractJpaDao;

import org.springframework.dao.DataAccessException;

import org.springframework.stereotype.Repository;

import org.springframework.transaction.annotation.Transactional;

/**
 * DAO to manage Facultyresearch entities.
 * 
 */
@Repository("FacultyresearchDAO")
@Transactional
public class FacultyresearchDAOImpl extends AbstractJpaDao<Facultyresearch>
		implements FacultyresearchDAO {

	/**
	 * Set of entity classes managed by this DAO.  Typically a DAO manages a single entity.
	 *
	 */
	private final static Set<Class<?>> dataTypes = new HashSet<Class<?>>(Arrays.asList(new Class<?>[] { Facultyresearch.class }));

	/**
	 * EntityManager injected by Spring for persistence unit rms
	 *
	 */
	@PersistenceContext(unitName = "rms")
	private EntityManager entityManager;

	/**
	 * Instantiates a new FacultyresearchDAOImpl
	 *
	 */
	public FacultyresearchDAOImpl() {
		super();
	}

	/**
	 * Get the entity manager that manages persistence unit 
	 *
	 */
	public EntityManager getEntityManager() {
		return entityManager;
	}

	/**
	 * Returns the set of entity classes managed by this DAO.
	 *
	 */
	public Set<Class<?>> getTypes() {
		return dataTypes;
	}

	/**
	 * JPQL Query - findFacultyresearchByTitleContaining
	 *
	 */
	@Transactional
	public Set<Facultyresearch> findFacultyresearchByTitleContaining(String title) throws DataAccessException {

		return findFacultyresearchByTitleContaining(title, -1, -1);
	}

	/**
	 * JPQL Query - findFacultyresearchByTitleContaining
	 *
	 */

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Facultyresearch> findFacultyresearchByTitleContaining(String title, int startResult, int maxRows) throws DataAccessException {
		Query query = createNamedQuery("findFacultyresearchByTitleContaining", startResult, maxRows, title);
		return new LinkedHashSet<Facultyresearch>(query.getResultList());
	}

	/**
	 * JPQL Query - findFacultyresearchByResearchStatus
	 *
	 */
	@Transactional
	public Set<Facultyresearch> findFacultyresearchByResearchStatus(String researchStatus) throws DataAccessException {

		return findFacultyresearchByResearchStatus(researchStatus, -1, -1);
	}

	/**
	 * JPQL Query - findFacultyresearchByResearchStatus
	 *
	 */

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Facultyresearch> findFacultyresearchByResearchStatus(String researchStatus, int startResult, int maxRows) throws DataAccessException {
		Query query = createNamedQuery("findFacultyresearchByResearchStatus", startResult, maxRows, researchStatus);
		return new LinkedHashSet<Facultyresearch>(query.getResultList());
	}

	/**
	 * JPQL Query - findFacultyresearchByMinumumRequirements
	 *
	 */
	@Transactional
	public Set<Facultyresearch> findFacultyresearchByMinumumRequirements(String minumumRequirements) throws DataAccessException {

		return findFacultyresearchByMinumumRequirements(minumumRequirements, -1, -1);
	}

	/**
	 * JPQL Query - findFacultyresearchByMinumumRequirements
	 *
	 */

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Facultyresearch> findFacultyresearchByMinumumRequirements(String minumumRequirements, int startResult, int maxRows) throws DataAccessException {
		Query query = createNamedQuery("findFacultyresearchByMinumumRequirements", startResult, maxRows, minumumRequirements);
		return new LinkedHashSet<Facultyresearch>(query.getResultList());
	}

	/**
	 * JPQL Query - findFacultyresearchByPrimaryKey
	 *
	 */
	@Transactional
	public Facultyresearch findFacultyresearchByPrimaryKey(Integer facultyResearchId) throws DataAccessException {

		return findFacultyresearchByPrimaryKey(facultyResearchId, -1, -1);
	}

	/**
	 * JPQL Query - findFacultyresearchByPrimaryKey
	 *
	 */

	@Transactional
	public Facultyresearch findFacultyresearchByPrimaryKey(Integer facultyResearchId, int startResult, int maxRows) throws DataAccessException {
		try {
			Query query = createNamedQuery("findFacultyresearchByPrimaryKey", startResult, maxRows, facultyResearchId);
			return (edu.unf.cen6940.domain.Facultyresearch) query.getSingleResult();
		} catch (NoResultException nre) {
			return null;
		}
	}

	/**
	 * JPQL Query - findAllFacultyresearchs
	 *
	 */
	@Transactional
	public Set<Facultyresearch> findAllFacultyresearchs() throws DataAccessException {

		return findAllFacultyresearchs(-1, -1);
	}

	/**
	 * JPQL Query - findAllFacultyresearchs
	 *
	 */

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Facultyresearch> findAllFacultyresearchs(int startResult, int maxRows) throws DataAccessException {
		Query query = createNamedQuery("findAllFacultyresearchs", startResult, maxRows);
		return new LinkedHashSet<Facultyresearch>(query.getResultList());
	}

	/**
	 * JPQL Query - findFacultyresearchByMinumumRequirementsContaining
	 *
	 */
	@Transactional
	public Set<Facultyresearch> findFacultyresearchByMinumumRequirementsContaining(String minumumRequirements) throws DataAccessException {

		return findFacultyresearchByMinumumRequirementsContaining(minumumRequirements, -1, -1);
	}

	/**
	 * JPQL Query - findFacultyresearchByMinumumRequirementsContaining
	 *
	 */

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Facultyresearch> findFacultyresearchByMinumumRequirementsContaining(String minumumRequirements, int startResult, int maxRows) throws DataAccessException {
		Query query = createNamedQuery("findFacultyresearchByMinumumRequirementsContaining", startResult, maxRows, minumumRequirements);
		return new LinkedHashSet<Facultyresearch>(query.getResultList());
	}

	/**
	 * JPQL Query - findFacultyresearchByResearchDescription
	 *
	 */
	@Transactional
	public Set<Facultyresearch> findFacultyresearchByResearchDescription(String researchDescription) throws DataAccessException {

		return findFacultyresearchByResearchDescription(researchDescription, -1, -1);
	}

	/**
	 * JPQL Query - findFacultyresearchByResearchDescription
	 *
	 */

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Facultyresearch> findFacultyresearchByResearchDescription(String researchDescription, int startResult, int maxRows) throws DataAccessException {
		Query query = createNamedQuery("findFacultyresearchByResearchDescription", startResult, maxRows, researchDescription);
		return new LinkedHashSet<Facultyresearch>(query.getResultList());
	}

	/**
	 * JPQL Query - findFacultyresearchByTitle
	 *
	 */
	@Transactional
	public Set<Facultyresearch> findFacultyresearchByTitle(String title) throws DataAccessException {

		return findFacultyresearchByTitle(title, -1, -1);
	}

	/**
	 * JPQL Query - findFacultyresearchByTitle
	 *
	 */

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Facultyresearch> findFacultyresearchByTitle(String title, int startResult, int maxRows) throws DataAccessException {
		Query query = createNamedQuery("findFacultyresearchByTitle", startResult, maxRows, title);
		return new LinkedHashSet<Facultyresearch>(query.getResultList());
	}

	/**
	 * JPQL Query - findFacultyresearchByResearchStatusContaining
	 *
	 */
	@Transactional
	public Set<Facultyresearch> findFacultyresearchByResearchStatusContaining(String researchStatus) throws DataAccessException {

		return findFacultyresearchByResearchStatusContaining(researchStatus, -1, -1);
	}

	/**
	 * JPQL Query - findFacultyresearchByResearchStatusContaining
	 *
	 */

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Facultyresearch> findFacultyresearchByResearchStatusContaining(String researchStatus, int startResult, int maxRows) throws DataAccessException {
		Query query = createNamedQuery("findFacultyresearchByResearchStatusContaining", startResult, maxRows, researchStatus);
		return new LinkedHashSet<Facultyresearch>(query.getResultList());
	}

	/**
	 * JPQL Query - findFacultyresearchByFacultyResearchId
	 *
	 */
	@Transactional
	public Facultyresearch findFacultyresearchByFacultyResearchId(Integer facultyResearchId) throws DataAccessException {

		return findFacultyresearchByFacultyResearchId(facultyResearchId, -1, -1);
	}

	/**
	 * JPQL Query - findFacultyresearchByFacultyResearchId
	 *
	 */

	@Transactional
	public Facultyresearch findFacultyresearchByFacultyResearchId(Integer facultyResearchId, int startResult, int maxRows) throws DataAccessException {
		try {
			Query query = createNamedQuery("findFacultyresearchByFacultyResearchId", startResult, maxRows, facultyResearchId);
			return (edu.unf.cen6940.domain.Facultyresearch) query.getSingleResult();
		} catch (NoResultException nre) {
			return null;
		}
	}

	/**
	 * JPQL Query - findFacultyresearchByResearchDescriptionContaining
	 *
	 */
	@Transactional
	public Set<Facultyresearch> findFacultyresearchByResearchDescriptionContaining(String researchDescription) throws DataAccessException {

		return findFacultyresearchByResearchDescriptionContaining(researchDescription, -1, -1);
	}

	/**
	 * JPQL Query - findFacultyresearchByResearchDescriptionContaining
	 *
	 */

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Facultyresearch> findFacultyresearchByResearchDescriptionContaining(String researchDescription, int startResult, int maxRows) throws DataAccessException {
		Query query = createNamedQuery("findFacultyresearchByResearchDescriptionContaining", startResult, maxRows, researchDescription);
		return new LinkedHashSet<Facultyresearch>(query.getResultList());
	}
	
	/**
	 * Advanced Research search
	 * 
	 * */
	@Transactional
	public Set<Facultyresearch> advancedSearch(Facultyresearch facultyresearch) throws DataAccessException {
		CriteriaBuilder cb = entityManager.getCriteriaBuilder();
		CriteriaQuery<Facultyresearch> cq = cb.createQuery(Facultyresearch.class);

		Root<Facultyresearch> from = cq.from(Facultyresearch.class);
		cq.select(from);
		
		List<Predicate> predicateList = new ArrayList<Predicate>();
		if(facultyresearch.getFaculty() != null){
			Predicate p = cb.equal(from.<String>get("faculty"), facultyresearch.getFaculty() );
			predicateList.add(p);
		}
		if(facultyresearch.getTitle() != null && !facultyresearch.getTitle().equals("")){
			Predicate p = cb.like(from.<String>get("title"), "%"+facultyresearch.getTitle()+"%");
			predicateList.add(p);
		}
		if(facultyresearch.getResearchDescription() != null && !facultyresearch.getResearchDescription().equals("")){
			Predicate p = cb.like(from.<String>get("researchDescription"), "%"+facultyresearch.getResearchDescription()+"%");
			predicateList.add(p);
		}
		
		if(facultyresearch.getResearchkeywords() != null && facultyresearch.getResearchkeywords().size() > 0){
			Join<Facultyresearch, Researchkeyword> categories =  from.join("researchkeywords");
			for(Researchkeyword researchkeyword : facultyresearch.getResearchkeywords()){
			
				Predicate p = cb.equal(categories.get("researchKeywordId"), researchkeyword.getResearchKeywordId() );
				predicateList.add(p);
			}
		}
		if(facultyresearch.getCategories() != null && facultyresearch.getCategories().size() > 0){
			Join<Facultyresearch, Category> categories =  from.join("categories");
			for(Category category: facultyresearch.getCategories()){
			
				Predicate p = cb.equal(categories.get("categoryId"), category.getCategoryId() );
				predicateList.add(p);
			}
		}
		Predicate[] predicates = new Predicate[predicateList.size()];
	    predicateList.toArray(predicates);
		cq.where(predicates);
		TypedQuery<Facultyresearch> typedQuery = entityManager.createQuery(cq);
		//List<Object> olist = typedQuery.getResultList();
		return new LinkedHashSet<Facultyresearch>(typedQuery.getResultList());
	}

	/**
	 * Used to determine whether or not to merge the entity or persist the entity when calling Store
	 * @see store
	 * 
	 *
	 */
	public boolean canBeMerged(Facultyresearch entity) {
		return true;
	}

	@Override
	public Set<Facultyresearch> searchByTerm(String term) throws DataAccessException {
		Query query = createQuery("from Facultyresearch myFacultyresearch where myFacultyresearch.title like ?1 or myFacultyresearch.researchDescription like ?1", -1, -1, "%"+term+"%");
		return new LinkedHashSet<Facultyresearch>(query.getResultList());
	}
	
	/**
	 * Search for Facultyresearch By Faculty Id
	 * */
	public Set<Facultyresearch> searchFacultyresearchByFacultyId(int id) throws DataAccessException{
		Query query = createQuery("select myFacultyresearch from Facultyresearch myFacultyresearch join myFacultyresearch.faculty myFaculty where myFaculty.facultyId = ?1", -1, -1, id);
		return new LinkedHashSet<Facultyresearch>(query.getResultList());
	}
	
	/**
	 * Search for Facultyresearch By Faculty Id
	 * */
	public Set<Facultyresearch> searchFacultyresearchByStudentId(int id) throws DataAccessException{
		Query query = createQuery("select myFacultyresearch from Facultyresearch myFacultyresearch join myFacultyresearch.facultyresearchtostudents myStudents where myStudents.studentId = ?1", -1, -1, id);
		return new LinkedHashSet<Facultyresearch>(query.getResultList());
	}
	
	/**
	 * Search for Facultyresearch By Faculty Id
	 * */
	public Set<Facultyresearch> searchFacultyresearchByStatus(String status) throws DataAccessException{
		Query query = createQuery("from Facultyresearch myFacultyresearch where myFacultyresearch.researchStatus = ?1", -1, -1, status);
		return new LinkedHashSet<Facultyresearch>(query.getResultList());
	}
}
